package com.ffapp.demo.map;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.PoiItem;
import com.ffapp.demo.R;
import com.ffapp.demo.map.util.AMapUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder> {

    private List<PoiItem> data;
    private Context context;

    private int selectedPosition = 0;

    OnAddressClickListener mListener;
    AMapLocation mLocation;


    public SearchResultAdapter(Context context, OnAddressClickListener listener) {
        this.context = context;
        this.mListener = listener;
        data = new ArrayList<>();
    }

    public void setData(List<PoiItem> data) {
        this.data = data;
    }
    public void setDataLocation(AMapLocation location) {
        this.mLocation = location;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public PoiItem getSeletedItem(){
        return data == null?null:data.get(selectedPosition);
    }

    public PoiItem getItem(int position){
        return data.get(position);
    }

    //创建新View，被LayoutManager所调用
    @Override
    public SearchResultAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_searchresult_list_item, viewGroup, false);
        return new SearchResultAdapter.ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(SearchResultAdapter.ViewHolder viewHolder, int position) {
        viewHolder.bind(position);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return  data==null?0:data.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.list_item)
        public View list_item;
        @BindView(R.id.image_check)
        public ImageView image_check;
        @BindView(R.id.text_title)
        public TextView text_title;
        @BindView(R.id.text_title_sub)
        public TextView text_title_sub;


        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bind(int position){
            PoiItem poiItem = data.get(position);
            text_title.setText(poiItem.getTitle());
//            text_title_sub.setText(AMapUtil.getAddressNameFromPoiItem(poiItem));
            String distance = "";
            if(mLocation != null && (poiItem.getLatLonPoint() != null)){
                distance = AMapUtil.getFriendlyLengthWechat((int) AMapUtils.calculateLineDistance(new LatLng(mLocation.getLatitude(),mLocation.getLongitude()),new LatLng(poiItem.getLatLonPoint().getLatitude(),poiItem.getLatLonPoint().getLongitude())));
            }
            text_title_sub.setText( (TextUtils.isEmpty(distance)?"":(distance+" | ") )+ AMapUtil.getAddressDesFromPoiItem(poiItem));
            image_check.setVisibility(position == selectedPosition ? View.VISIBLE : View.INVISIBLE);
            text_title_sub.setVisibility((position == 0 && poiItem.getPoiId().equals("regeo")) ? View.GONE : View.VISIBLE);
            list_item.setTag(position);
        }

        @OnClick(R.id.list_item)
        public void onItemClick(View v) {
            int position = (int) v.getTag();
//            PoiItem poiItem = data.get(position);
//            selectedPosition = position;
//            notifyDataSetChanged();
            if(mListener != null){
                mListener.onAddressClick(position);
            }
        }
    }

    public interface OnAddressClickListener{
        abstract void onAddressClick(int position);
    }
}